package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 护理计划Service业务层处理
 * 
 * @author liuyp
 * @date 2025-08-13
 */

@Transactional(rollbackFor = Exception.class)
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Resource
    private NursingPlanMapper nursingPlanMapper;
    
    @Resource
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {   NursingPlanVo vo = new NursingPlanVo();
        NursingPlan nursingPlan =nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectListByPlanId(id);
        vo.setProjectPlans(projectPlans);

        return vo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        save(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans != null && !projectPlans.isEmpty()){
            // 过滤掉projectId为空的项目
            projectPlans.removeIf(nursingProjectPlan -> nursingProjectPlan.getProjectId() == null);
            projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
            if (!projectPlans.isEmpty()) {
                nursingProjectPlanMapper.batchInsert(projectPlans);
            }
        }
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //如果客户端提交的数据中有关联的项目需要重
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            //先删除护理计划关联的护理项目
            nursingProjectPlanMapper.deleteByPlanId(dto.getId());
            //然后重新添加
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(dto.getId());
            }
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }

        //修改护理计划
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto, plan);
        return nursingPlanMapper.updateNursingPlan(plan);
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        nursingProjectPlanMapper.deleteByPlanId(id);
        return removeById(id)?1:0;
    }
}
